package com.david.gateway.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.GatewayFilterSpec;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.gateway.route.builder.UriSpec;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.function.Function;

@Slf4j
@Configuration
public class GatewayRouteConfig {

    /**
     * 构造不同的接口服务，转发到不同的服务。
     */
//    @Bean
//    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
//        return builder.routes()
//                .route(r -> r.path("/web/**")
//                        .uri("lb://IMD-BLOG-WEB")
//                        .id("imdBlogWeb")
//                ).build();
//    }


    static final Function<GatewayFilterSpec, UriSpec> GATEWAY_FILTER_SPECS = f -> f.stripPrefix(1);


    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder builder) {
        RouteLocatorBuilder.Builder routes = builder.routes();

        RouteLocatorBuilder.Builder serviceProvider = routes
                .route(r -> r.path("/david-ucenter/**").and().readBody(String.class, body -> true).filters(GATEWAY_FILTER_SPECS).uri("lb://david-ucenter").order(0))
                .route(r -> r.path("/david-ucenter/**").filters(GATEWAY_FILTER_SPECS).uri("lb://david-ucenter").order(0))

                .route(r -> r.path("/david-oms/**").and().readBody(String.class, body -> true).filters(GATEWAY_FILTER_SPECS).uri("lb://david-oms").order(1))
                .route(r -> r.path("/david-oms/**").filters(GATEWAY_FILTER_SPECS).uri("lb://david-oms").order(1))

                .route(r -> r.path("/david-redpacket/**").and().readBody(String.class, body -> true).filters(GATEWAY_FILTER_SPECS).uri("lb://david-redpacket").order(2))
                .route(r -> r.path("/david-redpacket/**").filters(GATEWAY_FILTER_SPECS).uri("lb://david-redpacket").order(2))

                .route(r -> r.path("/david-share/**").and().readBody(String.class, body -> true).filters(GATEWAY_FILTER_SPECS).uri("lb://david-share").order(3))
                .route(r -> r.path("/david-share/**").filters(GATEWAY_FILTER_SPECS).uri("lb://david-share").order(3))

                .route(r -> r.path("/david-mall/**").and().readBody(String.class, body -> true).filters(GATEWAY_FILTER_SPECS).uri("lb://david-mall").order(4))
                .route(r -> r.path("/david-mall/**").filters(GATEWAY_FILTER_SPECS).uri("lb://david-mall").order(4))

                .route(r -> r.path("/david-dbiz/**").and().readBody(String.class, body -> true).filters(GATEWAY_FILTER_SPECS).uri("lb://david-dbiz").order(5))
                .route(r -> r.path("/david-dbiz/**").filters(GATEWAY_FILTER_SPECS).uri("lb://david-dbiz").order(5))

                .route(r -> r.path("/david-dstorage/**").and().readBody(String.class, body -> true).filters(GATEWAY_FILTER_SPECS).uri("lb://david-dstorage").order(6))
                .route(r -> r.path("/david-dstorage/**").filters(GATEWAY_FILTER_SPECS).uri("lb://david-dstorage").order(6));
        return serviceProvider.build();
    }

}
